import axios from 'axios';
import qs from 'qs';

interface httpServer {
    get: object,
    post: object
}

const http: httpServer = {
    get: {},
    post: {}
};

/**
 * 封装post方法
 * @param api
 * @param data
 * @returns {Promise}
 */

http.post = (api: string, data: object = {}) => {
    let params = qs.stringify(data);
    return new Promise((resolve, reject) => {
        axios.post(api, params).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err);
            console.log(`请求出错:${err}`)
        })
    })
}


/**
 * 封装get方法
 * @param api
 * @returns {Promise}
 */

http.get = (api: string) => {
    return new Promise((resolve, reject) => {
        axios.get(api).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err);
            console.log(`请求出错:${err}`)
        })
    })
}

export default http;